﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.Algorithms.ConnectPool;

namespace HslCommunication.MQTT
{
	/// <summary>
	/// <b>[商业授权]</b> MqttSyncClient客户端的连接池类对象，用于共享当前的连接池，合理的动态调整连接对象，然后进行高效通信的操作，默认连接数无限大。<br />
	/// <b>[Authorization]</b> The connection pool class object of the MqttSyncClient is used to share the current connection pool, 
	/// reasonably dynamically adjust the connection object, and then perform efficient communication operations, 
	/// The default number of connections is unlimited
	/// </summary>
	/// <remarks>
	/// 本连接池用于提供高并发的读写性能，仅对商业授权用户开放。使用起来和<see cref="T:HslCommunication.MQTT.MqttSyncClient" />一致，但是更加的高性能，在密集型数据交互时，优势尤为明显。
	/// </remarks>
	// Token: 0x020000CA RID: 202
	public class MqttSyncClientPool
	{
		/// <summary>
		/// 通过MQTT连接参数实例化一个对象<br />
		/// Instantiate an object through MQTT connection parameters
		/// </summary>
		/// <param name="options">MQTT的连接参数信息</param>
		// Token: 0x06000F1E RID: 3870 RVA: 0x000594BC File Offset: 0x000576BC
		public MqttSyncClientPool(MqttConnectionOptions options)
		{
			this.connectionOptions = options;
			bool flag = Authorization.asdniasnfaksndiqwhawfskhfaiw();
			if (flag)
			{
				this.mqttConnectPool = new ConnectPool<IMqttSyncConnector>(() => new IMqttSyncConnector(options));
				this.mqttConnectPool.MaxConnector = int.MaxValue;
				return;
			}
			throw new Exception(StringResources.Language.InsufficientPrivileges);
		}

		/// <summary>
		/// 通过MQTT连接参数以及自定义的初始化方法来实例化一个对象<br />
		/// Instantiate an object through MQTT connection parameters and custom initialization methods
		/// </summary>
		/// <param name="options">MQTT的连接参数信息</param>
		/// <param name="initialize">自定义的初始化方法</param>
		// Token: 0x06000F1F RID: 3871 RVA: 0x00059530 File Offset: 0x00057730
		public MqttSyncClientPool(MqttConnectionOptions options, Action<MqttSyncClient> initialize)
		{
			this.connectionOptions = options;
			bool flag = Authorization.asdniasnfaksndiqwhawfskhfaiw();
			if (flag)
			{
				this.mqttConnectPool = new ConnectPool<IMqttSyncConnector>(delegate()
				{
					MqttSyncClient mqttSyncClient = new MqttSyncClient(options);
					initialize(mqttSyncClient);
					return new IMqttSyncConnector
					{
						SyncClient = mqttSyncClient
					};
				});
				this.mqttConnectPool.MaxConnector = int.MaxValue;
				return;
			}
			throw new Exception(StringResources.Language.InsufficientPrivileges);
		}

		/// <summary>
		/// 获取当前的连接池管理对象信息<br />
		/// Get current connection pool management object information
		/// </summary>
		// Token: 0x17000234 RID: 564
		// (get) Token: 0x06000F20 RID: 3872 RVA: 0x000595AB File Offset: 0x000577AB
		public ConnectPool<IMqttSyncConnector> GetMqttSyncConnectPool
		{
			get
			{
				return this.mqttConnectPool;
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Algorithms.ConnectPool.ConnectPool`1.MaxConnector" />
		// Token: 0x17000235 RID: 565
		// (get) Token: 0x06000F21 RID: 3873 RVA: 0x000595B3 File Offset: 0x000577B3
		// (set) Token: 0x06000F22 RID: 3874 RVA: 0x000595C0 File Offset: 0x000577C0
		public int MaxConnector
		{
			get
			{
				return this.mqttConnectPool.MaxConnector;
			}
			set
			{
				this.mqttConnectPool.MaxConnector = value;
			}
		}

		// Token: 0x06000F23 RID: 3875 RVA: 0x000595D0 File Offset: 0x000577D0
		private OperateResult<T> ConnectPoolExecute<T>(Func<MqttSyncClient, OperateResult<T>> exec)
		{
			bool flag = Authorization.asdniasnfaksndiqwhawfskhfaiw();
			if (flag)
			{
				IMqttSyncConnector availableConnector = this.mqttConnectPool.GetAvailableConnector();
				OperateResult<T> result = exec(availableConnector.SyncClient);
				this.mqttConnectPool.ReturnConnector(availableConnector);
				return result;
			}
			throw new Exception(StringResources.Language.InsufficientPrivileges);
		}

		// Token: 0x06000F24 RID: 3876 RVA: 0x00059624 File Offset: 0x00057824
		private OperateResult<T1, T2> ConnectPoolExecute<T1, T2>(Func<MqttSyncClient, OperateResult<T1, T2>> exec)
		{
			bool flag = Authorization.asdniasnfaksndiqwhawfskhfaiw();
			if (flag)
			{
				IMqttSyncConnector availableConnector = this.mqttConnectPool.GetAvailableConnector();
				OperateResult<T1, T2> result = exec(availableConnector.SyncClient);
				this.mqttConnectPool.ReturnConnector(availableConnector);
				return result;
			}
			throw new Exception(StringResources.Language.InsufficientPrivileges);
		}

		// Token: 0x06000F25 RID: 3877 RVA: 0x00059678 File Offset: 0x00057878
		[DebuggerStepThrough]
		private Task<OperateResult<T>> ConnectPoolExecuteAsync<T>(Func<MqttSyncClient, Task<OperateResult<T>>> exec)
		{
			MqttSyncClientPool.<ConnectPoolExecuteAsync>d__9<T> <ConnectPoolExecuteAsync>d__ = new MqttSyncClientPool.<ConnectPoolExecuteAsync>d__9<T>();
			<ConnectPoolExecuteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<T>>.Create();
			<ConnectPoolExecuteAsync>d__.<>4__this = this;
			<ConnectPoolExecuteAsync>d__.exec = exec;
			<ConnectPoolExecuteAsync>d__.<>1__state = -1;
			<ConnectPoolExecuteAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ConnectPoolExecuteAsync>d__9<T>>(ref <ConnectPoolExecuteAsync>d__);
			return <ConnectPoolExecuteAsync>d__.<>t__builder.Task;
		}

		// Token: 0x06000F26 RID: 3878 RVA: 0x000596C4 File Offset: 0x000578C4
		[DebuggerStepThrough]
		private Task<OperateResult<T1, T2>> ConnectPoolExecuteAsync<T1, T2>(Func<MqttSyncClient, Task<OperateResult<T1, T2>>> execAsync)
		{
			MqttSyncClientPool.<ConnectPoolExecuteAsync>d__10<T1, T2> <ConnectPoolExecuteAsync>d__ = new MqttSyncClientPool.<ConnectPoolExecuteAsync>d__10<T1, T2>();
			<ConnectPoolExecuteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<T1, T2>>.Create();
			<ConnectPoolExecuteAsync>d__.<>4__this = this;
			<ConnectPoolExecuteAsync>d__.execAsync = execAsync;
			<ConnectPoolExecuteAsync>d__.<>1__state = -1;
			<ConnectPoolExecuteAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ConnectPoolExecuteAsync>d__10<T1, T2>>(ref <ConnectPoolExecuteAsync>d__);
			return <ConnectPoolExecuteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.Read(System.String,System.Byte[],System.Action{System.Int64,System.Int64},System.Action{System.String,System.String},System.Action{System.Int64,System.Int64})" />
		// Token: 0x06000F27 RID: 3879 RVA: 0x00059710 File Offset: 0x00057910
		public OperateResult<string, byte[]> Read(string topic, byte[] payload, Action<long, long> sendProgress = null, Action<string, string> handleProgress = null, Action<long, long> receiveProgress = null)
		{
			return this.ConnectPoolExecute<string, byte[]>((MqttSyncClient m) => m.Read(topic, payload, sendProgress, handleProgress, receiveProgress));
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadString(System.String,System.String,System.Action{System.Int64,System.Int64},System.Action{System.String,System.String},System.Action{System.Int64,System.Int64})" />
		// Token: 0x06000F28 RID: 3880 RVA: 0x0005975C File Offset: 0x0005795C
		public OperateResult<string, string> ReadString(string topic, string payload, Action<long, long> sendProgress = null, Action<string, string> handleProgress = null, Action<long, long> receiveProgress = null)
		{
			return this.ConnectPoolExecute<string, string>((MqttSyncClient m) => m.ReadString(topic, payload, sendProgress, handleProgress, receiveProgress));
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadRpc``1(System.String,System.String)" />
		// Token: 0x06000F29 RID: 3881 RVA: 0x000597A8 File Offset: 0x000579A8
		public OperateResult<T> ReadRpc<T>(string topic, string payload)
		{
			return this.ConnectPoolExecute<T>((MqttSyncClient m) => m.ReadRpc<T>(topic, payload));
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadRpc``1(System.String,System.Object)" />
		// Token: 0x06000F2A RID: 3882 RVA: 0x000597DC File Offset: 0x000579DC
		public OperateResult<T> ReadRpc<T>(string topic, object payload)
		{
			return this.ConnectPoolExecute<T>((MqttSyncClient m) => m.ReadRpc<T>(topic, payload));
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadRpcApis" />
		// Token: 0x06000F2B RID: 3883 RVA: 0x0005980F File Offset: 0x00057A0F
		public OperateResult<MqttRpcApiInfo[]> ReadRpcApis()
		{
			return this.ConnectPoolExecute<MqttRpcApiInfo[]>((MqttSyncClient m) => m.ReadRpcApis());
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadRpcApiLog(System.String)" />
		// Token: 0x06000F2C RID: 3884 RVA: 0x00059838 File Offset: 0x00057A38
		public OperateResult<long[]> ReadRpcApiLog(string api)
		{
			return this.ConnectPoolExecute<long[]>((MqttSyncClient m) => m.ReadRpcApiLog(api));
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadRetainTopics" />
		// Token: 0x06000F2D RID: 3885 RVA: 0x00059864 File Offset: 0x00057A64
		public OperateResult<string[]> ReadRetainTopics()
		{
			return this.ConnectPoolExecute<string[]>((MqttSyncClient m) => m.ReadRetainTopics());
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadTopicPayload(System.String,System.Action{System.Int64,System.Int64})" />
		// Token: 0x06000F2E RID: 3886 RVA: 0x0005988C File Offset: 0x00057A8C
		public OperateResult<MqttClientApplicationMessage> ReadTopicPayload(string topic, Action<long, long> receiveProgress = null)
		{
			return this.ConnectPoolExecute<MqttClientApplicationMessage>((MqttSyncClient m) => m.ReadTopicPayload(topic, receiveProgress));
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClientPool.Read(System.String,System.Byte[],System.Action{System.Int64,System.Int64},System.Action{System.String,System.String},System.Action{System.Int64,System.Int64})" />
		// Token: 0x06000F2F RID: 3887 RVA: 0x000598C0 File Offset: 0x00057AC0
		[DebuggerStepThrough]
		public Task<OperateResult<string, byte[]>> ReadAsync(string topic, byte[] payload, Action<long, long> sendProgress = null, Action<string, string> handleProgress = null, Action<long, long> receiveProgress = null)
		{
			MqttSyncClientPool.<ReadAsync>d__19 <ReadAsync>d__ = new MqttSyncClientPool.<ReadAsync>d__19();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string, byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.topic = topic;
			<ReadAsync>d__.payload = payload;
			<ReadAsync>d__.sendProgress = sendProgress;
			<ReadAsync>d__.handleProgress = handleProgress;
			<ReadAsync>d__.receiveProgress = receiveProgress;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadAsync>d__19>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClientPool.ReadString(System.String,System.String,System.Action{System.Int64,System.Int64},System.Action{System.String,System.String},System.Action{System.Int64,System.Int64})" />
		// Token: 0x06000F30 RID: 3888 RVA: 0x0005992C File Offset: 0x00057B2C
		[DebuggerStepThrough]
		public Task<OperateResult<string, string>> ReadStringAsync(string topic, string payload, Action<long, long> sendProgress = null, Action<string, string> handleProgress = null, Action<long, long> receiveProgress = null)
		{
			MqttSyncClientPool.<ReadStringAsync>d__20 <ReadStringAsync>d__ = new MqttSyncClientPool.<ReadStringAsync>d__20();
			<ReadStringAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string, string>>.Create();
			<ReadStringAsync>d__.<>4__this = this;
			<ReadStringAsync>d__.topic = topic;
			<ReadStringAsync>d__.payload = payload;
			<ReadStringAsync>d__.sendProgress = sendProgress;
			<ReadStringAsync>d__.handleProgress = handleProgress;
			<ReadStringAsync>d__.receiveProgress = receiveProgress;
			<ReadStringAsync>d__.<>1__state = -1;
			<ReadStringAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadStringAsync>d__20>(ref <ReadStringAsync>d__);
			return <ReadStringAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClientPool.ReadRpc``1(System.String,System.String)" />
		// Token: 0x06000F31 RID: 3889 RVA: 0x00059998 File Offset: 0x00057B98
		[DebuggerStepThrough]
		public Task<OperateResult<T>> ReadRpcAsync<T>(string topic, string payload)
		{
			MqttSyncClientPool.<ReadRpcAsync>d__21<T> <ReadRpcAsync>d__ = new MqttSyncClientPool.<ReadRpcAsync>d__21<T>();
			<ReadRpcAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<T>>.Create();
			<ReadRpcAsync>d__.<>4__this = this;
			<ReadRpcAsync>d__.topic = topic;
			<ReadRpcAsync>d__.payload = payload;
			<ReadRpcAsync>d__.<>1__state = -1;
			<ReadRpcAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadRpcAsync>d__21<T>>(ref <ReadRpcAsync>d__);
			return <ReadRpcAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClientPool.ReadRpc``1(System.String,System.Object)" />
		// Token: 0x06000F32 RID: 3890 RVA: 0x000599EC File Offset: 0x00057BEC
		[DebuggerStepThrough]
		public Task<OperateResult<T>> ReadRpcAsync<T>(string topic, object payload)
		{
			MqttSyncClientPool.<ReadRpcAsync>d__22<T> <ReadRpcAsync>d__ = new MqttSyncClientPool.<ReadRpcAsync>d__22<T>();
			<ReadRpcAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<T>>.Create();
			<ReadRpcAsync>d__.<>4__this = this;
			<ReadRpcAsync>d__.topic = topic;
			<ReadRpcAsync>d__.payload = payload;
			<ReadRpcAsync>d__.<>1__state = -1;
			<ReadRpcAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadRpcAsync>d__22<T>>(ref <ReadRpcAsync>d__);
			return <ReadRpcAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClientPool.ReadRpcApis" />
		// Token: 0x06000F33 RID: 3891 RVA: 0x00059A40 File Offset: 0x00057C40
		[DebuggerStepThrough]
		public Task<OperateResult<MqttRpcApiInfo[]>> ReadRpcApisAsync()
		{
			MqttSyncClientPool.<ReadRpcApisAsync>d__23 <ReadRpcApisAsync>d__ = new MqttSyncClientPool.<ReadRpcApisAsync>d__23();
			<ReadRpcApisAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<MqttRpcApiInfo[]>>.Create();
			<ReadRpcApisAsync>d__.<>4__this = this;
			<ReadRpcApisAsync>d__.<>1__state = -1;
			<ReadRpcApisAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadRpcApisAsync>d__23>(ref <ReadRpcApisAsync>d__);
			return <ReadRpcApisAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClientPool.ReadRpcApiLog(System.String)" />
		// Token: 0x06000F34 RID: 3892 RVA: 0x00059A84 File Offset: 0x00057C84
		[DebuggerStepThrough]
		public Task<OperateResult<long[]>> ReadRpcApiLogAsync(string api)
		{
			MqttSyncClientPool.<ReadRpcApiLogAsync>d__24 <ReadRpcApiLogAsync>d__ = new MqttSyncClientPool.<ReadRpcApiLogAsync>d__24();
			<ReadRpcApiLogAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<long[]>>.Create();
			<ReadRpcApiLogAsync>d__.<>4__this = this;
			<ReadRpcApiLogAsync>d__.api = api;
			<ReadRpcApiLogAsync>d__.<>1__state = -1;
			<ReadRpcApiLogAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadRpcApiLogAsync>d__24>(ref <ReadRpcApiLogAsync>d__);
			return <ReadRpcApiLogAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadRetainTopics" />
		// Token: 0x06000F35 RID: 3893 RVA: 0x00059AD0 File Offset: 0x00057CD0
		[DebuggerStepThrough]
		public Task<OperateResult<string[]>> ReadRetainTopicsAsync()
		{
			MqttSyncClientPool.<ReadRetainTopicsAsync>d__25 <ReadRetainTopicsAsync>d__ = new MqttSyncClientPool.<ReadRetainTopicsAsync>d__25();
			<ReadRetainTopicsAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string[]>>.Create();
			<ReadRetainTopicsAsync>d__.<>4__this = this;
			<ReadRetainTopicsAsync>d__.<>1__state = -1;
			<ReadRetainTopicsAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadRetainTopicsAsync>d__25>(ref <ReadRetainTopicsAsync>d__);
			return <ReadRetainTopicsAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttSyncClient.ReadTopicPayload(System.String,System.Action{System.Int64,System.Int64})" />
		// Token: 0x06000F36 RID: 3894 RVA: 0x00059B14 File Offset: 0x00057D14
		[DebuggerStepThrough]
		public Task<OperateResult<MqttClientApplicationMessage>> ReadTopicPayloadAsync(string topic, Action<long, long> receiveProgress = null)
		{
			MqttSyncClientPool.<ReadTopicPayloadAsync>d__26 <ReadTopicPayloadAsync>d__ = new MqttSyncClientPool.<ReadTopicPayloadAsync>d__26();
			<ReadTopicPayloadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<MqttClientApplicationMessage>>.Create();
			<ReadTopicPayloadAsync>d__.<>4__this = this;
			<ReadTopicPayloadAsync>d__.topic = topic;
			<ReadTopicPayloadAsync>d__.receiveProgress = receiveProgress;
			<ReadTopicPayloadAsync>d__.<>1__state = -1;
			<ReadTopicPayloadAsync>d__.<>t__builder.Start<MqttSyncClientPool.<ReadTopicPayloadAsync>d__26>(ref <ReadTopicPayloadAsync>d__);
			return <ReadTopicPayloadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000F37 RID: 3895 RVA: 0x00059B66 File Offset: 0x00057D66
		public override string ToString()
		{
			return string.Format("MqttSyncClientPool[{0}]", this.mqttConnectPool.MaxConnector);
		}

		// Token: 0x04000418 RID: 1048
		private MqttConnectionOptions connectionOptions;

		// Token: 0x04000419 RID: 1049
		private ConnectPool<IMqttSyncConnector> mqttConnectPool;
	}
}
